/*=============================================================================
// File Name:   pt_thread.h
// Author:      Wood Wang
// Date:        2022/10/1
// Version:     v1.0

=============================================================================*/
/* Includes -----------------------------------------------------------------*/
#ifndef __PT_THREAD_H
#define __PT_THREAD_H

#include "stdint.h"

#define pt_irq_safe     for(uint32_t _b=__get_PRIMASK(),_a=0 ; ((_a++) == 0)? (__disable_irq(),1):0 ; __set_PRIMASK(_b))


typedef	struct	fsm_pt_t
{
	volatile uint32_t		state;
	volatile uint32_t		time;
	volatile uint32_t		evt;
             void *         p_user;
}fsm_pt_t;

#define PT_BEGIN(pt)		switch ((pt)->state) { case 0:
#define PT_ENTRY(pt)		((pt)->state) = __LINE__; case __LINE__:
#define PT_END(pt)			}


#define PT_WAIT(pt)				\
		do{	(pt)->evt = 0;		\
			PT_ENTRY(pt)		\
			if((pt)->evt == 0){return;}	\
		}while(0)

#define PT_WAIT_TIME(pt,tick)	\
		do{						\
			(pt)->time = tick; 	\
			PT_ENTRY(pt)		\
			if((pt)->time){return;}	\
		}while(0)

#define PT_DELAY_TICK(pt,tick)	\
		do{						\
			(pt)->time = tick; 	\
			PT_ENTRY(pt)		\
			if((pt)->time){(pt)->time--; return;}	\
		}while(0)	
        
#define PT_WAIT_EVT(pt,evt)		\
		do{	(pt)->evt = 0;		\
			PT_ENTRY(pt)		\
			if((pt)->evt != evt){return;}	\
		}while(0)

#define PT_WAIT_EVT_TIMEOUT(pt,evt,tick)	\
		do{	(pt)->evt = 0;		\
            (pt)->time = tick;	\
			PT_ENTRY(pt)		\
			if(((pt)->evt != evt)&&((pt)->time)){return;}	\
		}while(0)



#endif


